<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/15
 * Time: 17:17
 */

namespace Admin\Controller;


use Admin\Model\Page;
use Common\Utils\Constants;
use Common\Utils\ImageUtils;
use Think\Exception;

class SettingsController extends BaseController
{
    public function __construct($baseModel)
    {
        parent::__construct(D('Config'));
        $this->bcItemPush('基本设置', U('Settings/index'));
    }

    public function index()
    {
        $config = D('Config')->select();
        $configList = array();
        foreach ($config as $key => $value){
            $configList[$config[$key]['name']] = $config[$key]['value'];
        }
        $this->assign('config',$configList);
        $this->display();
    }

    protected function doEditBefore($data)
    {
        $this->checkName($data, '修改');
    }

    /**
     * 跳转到游戏说明界面
     */
    public function gameShows(){
        $config = D('Config')->where(array('name'=>Constants::CONFIG_GAME_SHOWS))->find();
        $configs[$config['name']] = html_entity_decode($config['value']);
        $this->assign('config',$configs);
        $this->bcItemPush('游戏说明', U('Settings/gameShows'));
        $this->display();
    }

    /**
     * 跳转到客服说明界面
     */
    public function customerShows(){
        $config = D('Config')->where(array('name'=>Constants::CONFIG_CUSTOMER_SPECIFICATION))->find();
        $configs[$config['name']] = html_entity_decode($config['value']);
        $this->assign('config',$configs);
        $this->bcItemPush('客服说明', U('Settings/customerShows'));
        $this->display();
    }

    /**
     * 跳转到钻石作用
     */
    public function integralEffect(){
        $config = D('Config')->where(array('name'=>Constants::CONFIG_INTEGRAL_EFFECT))->find();
        $this->assign('title','钻石作用说明');
        $this->assign('value',html_entity_decode($config['value']));
        $this->assign('key_name',$config['name']);
        $this->bcItemPush('钻石作用说明', U('Settings/integralEffect'));
        $this->display('editSetting');
    }

    /**
     * 跳转到擂台规则
     */
    public function roomRules(){
        $config = D('Config')->where(array('name'=>Constants::CONFIG_ROOM_RULES))->find();
        $this->assign('title','擂台规则说明');
        $this->assign('value',html_entity_decode($config['value']));
        $this->assign('key_name',$config['name']);
        $this->bcItemPush('擂台规则说明', U('Settings/roomRules'));
        $this->display('editSetting');
    }

    /**
     * 跳转到赚取钻石说明界面
     */
    public function earnIntegral(){
        $config = D('Config')->where(array('name'=>Constants::CONFIG_EARN_INTEGRAL))->find();
        $this->assign('title','赚取钻石说明');
        $this->assign('value',html_entity_decode($config['value']));
        $this->assign('key_name',$config['name']);
        $this->bcItemPush('擂台规则说明', U('Settings/earnIntegral'));
        $this->display('editSetting');
    }

    /**
     * 跳转到金币作用
     */
    public function goldEffect(){
        $config = D('Config')->where(array('name'=>Constants::CONFIG_GOLD_EFFECT))->find();
        $this->assign('title','赚取金币说明');
        $this->assign('value',html_entity_decode($config['value']));
        $this->assign('key_name',$config['name']);
        $this->bcItemPush('赚取金币说明', U('Settings/goldEffect'));
        $this->display('editSetting');
    }

    /**
     * 跳转到推广介绍
     */
    public function promotionShow(){
        $config = D('Config')->where(array('name'=>Constants::CONFIG_PROMOTION_SHOW))->find();
        $this->assign('title','推广介绍说明');
        $this->assign('value',html_entity_decode($config['value']));
        $this->assign('key_name',$config['name']);
        $this->bcItemPush('推广介绍', U('Settings/promotionShow'));
        $this->display('editSetting');
    }

    /**
     * 跳转到抽奖规则编辑界面
     */
    public function lotteryRules(){
        $config = D('Config')->where(array('name'=>Constants::CONFIG_LOTTERY_RULES))->find();
        $configs[$config['name']] = html_entity_decode($config['value']);
        $this->assign('config',$configs);
        $this->bcItemPush('抽奖规则', U('Settings/lotteryRules'));
        $this->display();
    }

    /**
     * 跳转到二维码设置界面
     */
    public function qrCode(){
        $config = D('Config')->select();
        $configList = array();
        foreach ($config as $key => $value){
            $configList[$config[$key]['name']] = $config[$key]['value'];
        }
        $this->assign('config',$configList);
        $this->bcItemPush('二维码设置', U('Settings/qrCode'));
        $this->display();
    }

    public function edit()
    {
//        $configs = I('config');
        $configs = $_POST['config'];
        $model =  M();
        try {
            $model->startTrans();
            foreach ($configs as $key => $value) {
                $where['name'] = ':name';
                $bind[':name'] = $key;
//                $this->ajaxReturn($value);
                $config = $model->table('t_config')->where($where)->bind($bind)->find();
                    if ($config['value'] == $value) {
                        continue;
                    }
                    if($config['name'] == Constants::CONFIG_ADD_ROOM_IMAGE){
                        $utils = new ImageUtils();
                        $utils->thum_images($config['name'],375,185,'room',$config['value'],true);//重新生成缩略图
                    }
                    if($config['name'] == Constants::CONFIG_PAGEVIEWS_PROPORTION){
                        $this->updatePageViewsProportion($model);
                    }
                    $config['value'] = $value;
                $model->table('t_config')->save($config);
                $model->commit();
                $this->ajaxReturn(array('success'=>true,'message'=>'修改成功'));

            }
        }catch (Exception $e){
            $model->rollback();
            $this->ajaxReturn(array('success'=>false,'message'=>'修改失败'.$e->getMessage()));
        }
    }

    private function updatePageViewsProportion($model){
        $model->table('t_user')->where(array('is_first'=>0))->setField('count_pageviews',0);
    }

    public function editField(){
        $name = I('name');
        $value = $_POST['value'];
        $config = D('Config')->where(array('name'=>$name))->find();
        if(!$config){
            $this->ajaxReturn(array('success'=>false,'message'=>'修改失败'));
        }
        D('Config')->where(array('name'=>$name))->setField('value',$value);
        $this->ajaxReturn(array('success'=>true,'message'=>'修改成功'));
    }

    /**
     * 配置名称不能重复
     * @param $data
     * @param $option
     */
    private function checkName($data, $option)
    {
        $where['name'] = ':name';
        $count = $this->baseModel->where($where)->bind(':name', $data['name'])->count();
        if (($option == '修改' && $count >= 2) || ($option == '添加' && $count >= 1)) {
            parent::retJson(false, $option . '失败,配置名称不能重复');
        }
    }
}